/* 
 * This code is part of the smartlink project, see
 * 
 * 		http://code.google.com/p/smartlink/.
 * 
 * Copyright (C) 2008 Oliver Noelle
 * 
 * SmartLink is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * SmartLink is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with SmartLink.  If not, see <http://www.gnu.org/licenses/>.
 **/
package com.google.bookmarkswrapper;

import org.smartlink.Server;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Browser;
import android.util.Log;

/**
 * Thin wrapper around the Bookmarks application to smartlink-enable it.
 */
public class BookmarksWrapper extends Activity {

	private static final String TAG = "BookmarksWrapper";
	/** request code for picking a target item */
	protected static final int PICK_TARGET_REQUEST_CODE = 1;

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
	}

	@Override
	protected void onStart() {
		super.onStart();
		Intent intent = getIntent();
		String action = intent.getAction();
		if (action == null) {
			// eg. "launched" via eclipse
			finish();
			return;
		}
		if (action.equals(Server.PICK_TARGET_ACTION)) {
			// Uri uri = android.provider.Browser.BOOKMARKS_URI;
			Intent targetIntent = new Intent();
			// the browser component is hardcoded
			targetIntent.setClassName("com.google.android.browser", "com.google.android.browser.BrowserBookmarksPage");
			startSubActivity(targetIntent, PICK_TARGET_REQUEST_CODE);
		} else {
			Log.w(TAG, "warning: unexpected action: " + action);
			return;
		}
	}

	/** Listen for results of picking targets. */
	protected void onActivityResult(int requestCode, int resultCode, String data, Bundle extras) {
		Log.w(TAG, "result code: " + resultCode);
		Log.w(TAG, "data: " + data);

		if (resultCode == RESULT_CANCELED) {
			// picking a Bookmark was canceled
			setResult(RESULT_CANCELED);
			finish();
			return;
		}
		switch (requestCode) {
		case PICK_TARGET_REQUEST_CODE:
			// a smartlink server app has to provide two strings as a result of
			// picking a target:
			// 1. the target uri of the data item
			// 2. a descriptive text of the data item
			Bundle bundle = new Bundle(2);
			bundle.putString(Server.EXTRA_ITEM_TARGET, data);
			bundle.putString(Server.EXTRA_ITEM_DESCRIPTION, getBookmarkName(data));
			setResult(RESULT_OK, data, bundle);
			finish();
			return;
		default:
			Log.w(TAG, "unexpected result code: " + resultCode);
		}
	}

	/**
	 * @return the title of a bookmark identified by the url it is pointing to.
	 */
	private String getBookmarkName(String url) {
		final String[] projection = { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL,
				Browser.BookmarkColumns.BOOKMARK, };
		Cursor cur = getContentResolver().query(Browser.BOOKMARKS_URI, projection, null, null, null);
		if (cur == null) {
			return null;
		}
		while (cur.next()) {
			if (cur.getString(1).equals(url)) {
				return cur.getString(0);
			}
		}
		return null;
	}
}
